package work04;
import java.util.List;
import java.io.*;
import java.util.ArrayList;
public class Test04 {
    public static void main(String[] args) {
        Staff s1 = new Staff("Tom",30, 5000);
        List<Staff> list = new ArrayList<>();
        list.add(s1);
        File f1 = new File("doc/a.txt");
        saveStaffToFile(s1,f1);
        List<Staff> list1 = readStaffFromFile(f1);

    }

    static void saveStaffToFile(Staff staff, File file){
        try(ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file)))
        {
            oos.writeObject(staff);
            oos.flush();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    static List<Staff> readStaffFromFile(File file){
        try(ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file)))
        {
            List<Staff> list = (List<Staff>) ois.readObject();
            return list;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }
}

class Staff implements Serializable {
    private String name;
    private int age;
    private double salary;

    public Staff() {
    }

    public Staff(String name, int age, double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return "Staff{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", salary=" + salary +
                '}';
    }

}
